package com.cyy.ultra_ptr_demo.avtivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cyy.ultra_ptr_demo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class ListViewActivity extends AppCompatActivity {

    @InjectView(R.id.prt_id)
    ListView prtId;
    @InjectView(R.id.ptr_fl)
    PtrFrameLayout ptrFl;
    private List<String> mDatas;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        ButterKnife.inject(this);
        /**
         * 初始化数据源
         */
        initData();
        //设置下拉框的阻尼系数。
        ptrFl.setResistance(1.2f);
        //是否是下拉刷新，，true下拉到一定位置刷新，fasle释放刷新。
        ptrFl.setPullToRefresh(false);
        //头部高度的基本去刷新
        ptrFl.setRatioOfHeaderHeightToRefresh(1.2f);
        //刷新的时候是否保持头部
        ptrFl.setKeepHeaderWhenRefresh(true);
        //恢复到头部位置的时常
        ptrFl.setDurationToClose(200);
        //头部整体关闭的时长
        ptrFl.setDurationToCloseHeader(1000);

        /**
         * 设置监听事件
         * 1:用来监听关闭的时间
         */
        ptrFl.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptrFl.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.clear();
                        for (int i = 0; i <20 ; i++) {
                            mDatas.add("刷新的新的数据"+i);
                            myAdapter.notifyDataSetChanged();
                        }
                        //关闭刷新
                        ptrFl.refreshComplete();
                    }
                },2000);
            }
        });
        /*//上拉加载更多
        ptrFl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //设置上拉加载的内容
                        for (int i = 0; i <10 ; i++) {
                            mDatas.add("上拉加载的数据"+String.valueOf(i));
                        }
                        //通知适配器数据源发生了变化
                        myAdapter.notifyDataSetChanged();
                        //加载完成通知刷新完成
                        ptrFl.loadMoreComplete(true);
                        Toast.makeText(ListViewActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
                    }
                },2000);
            }
        });*/
    }
    //初始化数据源
    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i <50 ; i++) {
            mDatas.add("哈哈哈哈哈：：："+i);
        }
        myAdapter = new MyAdapter();
        prtId.setAdapter(myAdapter);
    }


    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = new TextView(ListViewActivity.this);
            view.setText(mDatas.get(position));
            view.setTextSize(20);
            return view;
        }
    }
}
